#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QBluetoothLocalDevice"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowIcon(QIcon("://icon/VStart.ico"));

    //连接设备按钮点击事件
    this->btConnect = new BluetoothConnect();
    connect(ui->btnConnectDevice,&QPushButton::clicked,[=](){
        //this->hide();
        btConnect->show();
    });

    //监听连接设备窗口发送的连接服务信号
    connect(btConnect,&BluetoothConnect::toConectCharacteristic,this,&MainWindow::ConnectCharacteristic);

    //数据发送按钮点击事件
    connect(ui->btnSend,&QPushButton::clicked,[=](){
        //发送数据
        //qDebug() << "btConnect->m_service->state = " << btConnect->m_service;
        if (btConnect->m_service == nullptr)
            return;

        btConnect->m_service->writeCharacteristic(btConnect->m_characteristic, ui->leSendData->text().toUtf8(),QLowEnergyService::WriteMode::WriteWithoutResponse);
        ui->leRxTxState->setText("发送完成");
    });

    //关闭数据接收按钮点击事件
    connect(ui->btnClose,&QPushButton::clicked,[=](){
        if(this->isClose)
        {
            ui->btnClose->setText("开启");
            this->isClose = false;
        }
        else
        {
            ui->btnClose->setText("关闭");
            this->isClose = true;
        }
    });

    //清除数据接受区内容按钮点击事件
    connect(ui->btnClear,&QPushButton::clicked,[=](){
        ui->tbRevData->clear();
    });
}

void MainWindow::ConnectCharacteristic(QLowEnergyService * m_service)
{
    // 通过监听特征对象的变化
    //写的和读取到的都会进入到这个位置
    connect(m_service,&QLowEnergyService::characteristicChanged,[=](QLowEnergyCharacteristic c,QByteArray value) {
        //        qDebug() << "characteristicChanged state change::";
        //        qDebug() << "value length::" << value.length();
        //        qDebug() << "value length::" << value;
        if(this->isClose)
        {
            ui->tbRevData->append(value);
        }
    });

    //没有写响应属性的Characteristic不会进入这个连接
    //    connect(m_service,&QLowEnergyService::characteristicWritten,[=](QLowEnergyCharacteristic c,QByteArray value) {
    qDebug() << "characteristicChanged state change::";
    //qDebug() << "value length::" << value.length();
    //qDebug() << "value length::" << value;
    //    });

    //service错误监听
    connect(m_service,&QLowEnergyService::errorOccurred,[=](QLowEnergyService::ServiceError newErrorr){
        if(QLowEnergyService::NoError == newErrorr)
        {
            qDebug() <<"没有发生错误。";
            ui->leRxTxState->setText("没有发生错误。");
        }
        if(QLowEnergyService::OperationError==newErrorr)
        {
            qDebug() <<"错误: 当服务没有准备好时尝试进行操作!";
            ui->leRxTxState->setText("错误: 当服务没有准备好时尝试进行操作");
        }
        if(QLowEnergyService::CharacteristicReadError==newErrorr)
        {
            qDebug() <<"尝试读取特征值失败!";
            ui->leRxTxState->setText("尝试读取特征值失败");
        }
        if(QLowEnergyService::CharacteristicWriteError==newErrorr)
        {
            qDebug() <<"尝试为特性写入新值失败!";
            ui->leRxTxState->setText("尝试为特性写入新值失败");
        }
        if(QLowEnergyService::DescriptorReadError==newErrorr)
        {
            qDebug() <<"尝试读取描述符值失败!";
            ui->leRxTxState->setText("尝试读取描述符值失败");
        }
        if(QLowEnergyService::DescriptorWriteError==newErrorr)
        {
            qDebug() <<" 尝试向描述符写入新值失败!";
            ui->leRxTxState->setText("尝试向描述符写入新值失败");
        }
        if(QLowEnergyService::UnknownError==newErrorr)
        {
            qDebug() <<"与服务交互时发生未知错误!";
            ui->leRxTxState->setText("与服务交互时发生未知错误");
        }

    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

